package aam.controller;

import java.io.File;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import aam.model.BoardListVO;
import aam.model.SubBoardListVO;
import aam.model.board.BoardVO;
import aam.model.member.MemberVO;
import aam.service.BoardService;
import aam.service.MemberService;
import aam.service.SubBoardService;

@Controller
public class BoardController {
	@Resource(name="boardServiceImpl")
	private BoardService boardService;
	@Resource(name="subBoardServiceImpl")
	private SubBoardService subBoardService;
	@Resource(name="memberServiceImpl")
	private MemberService memberService;
	
	public BoardController() {
		super();
	}

	private String path="C:\\java-kosta\\final-project\\project-aam\\WebContent\\img\\upload\\";//upload path
	public BoardController(BoardService boardService,String path){
		this.boardService=boardService;
		this.path=path;
	}
	private void deleteFile(String newfilename){
		File f=new File(path+newfilename);
		boolean result=f.delete();
	}
	
	@RequestMapping("/board_home.do")
	public ModelAndView getAllViewMain() throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.addObject("list",boardService.getAllViewMain());
		mv.addObject("vlist",boardService.getAllViewMainByView());
		mv.addObject("slist",boardService.getViewMainByShare());
		mv.addObject("rlist",boardService.getViewMainByReview());
		mv.addObject("dlist",subBoardService.selectReport());
		mv.setViewName("home");
		return mv;
	}
	@RequestMapping("/allView_board.do")
	public ModelAndView allView(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView();
		int boardNo = Integer.parseInt(request.getParameter("boardNo"));
		String page = request.getParameter("page");
		boolean genreFlag = false;
		if(boardNo==13||boardNo==20){
			mv.addObject("boardName", boardService.getBoardNameByBoardNo(boardNo)); 
			mv.addObject("genreList", boardService.getGenreList());
			genreFlag = true;
			mv.setViewName("genreboard.board");
		}else{
			mv.addObject("boardName", boardService.getBoardNameByBoardNo(boardNo));
			mv.setViewName("basic.board");
		}
		BoardListVO lvo = boardService.getAllView(page, boardNo, genreFlag);
		mv.addObject("lvo", lvo);
		mv.addObject("boardNo", boardNo);
		if(page==null){
			page = "1";
		}
		mv.addObject("page", page);
		return mv;
	}
	
	@RequestMapping("/write_board.do")
	public ModelAndView write(HttpServletRequest request, HttpSession session, BoardVO bvo) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("vo");
		int boardNo = Integer.parseInt(request.getParameter("boardKind"));
		if(boardNo==20){
			String genreNo = request.getParameter("genreNo");
			bvo.setGenre(genreNo);
			MultipartFile mfile=bvo.getUploadFile();
			if(mfile.isEmpty()==false){
				String fileName=mfile.getOriginalFilename();
				bvo.setOrgFileName(fileName);
				String newfilename=System.currentTimeMillis()+"_"+fileName;
				bvo.setNewFileName(newfilename);
				File copyFile=new File(path+newfilename);
				mfile.transferTo(copyFile);
			}
		}else if(boardNo==21){  
			MultipartFile mfile=bvo.getUploadFile();
			if(mfile.isEmpty()==false){
				String fileName=mfile.getOriginalFilename();
				bvo.setOrgFileName(fileName);
				String newfilename=System.currentTimeMillis()+"_"+fileName;
				bvo.setNewFileName(newfilename);
				File copyFile=new File(path+newfilename);
				mfile.transferTo(copyFile);
			}
		}else if(boardNo!=21){
			String content = bvo.getContent();
			content = content.replace("\r\n","<br>");
			bvo.setContent(content);
		}
		String page=request.getParameter("page");	
		bvo.setMemberVO(mvo);
		boardService.write(bvo);
		
		session.setAttribute("vo", memberService.login(mvo));
		
		return new ModelAndView("redirect:getContentNo_board.do?no="+bvo.getNo()+"&boardNo="+boardNo+"&page="+page);
	}
	
	@RequestMapping("/getContentView_board.do")
	public ModelAndView getContentView(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception{
		ModelAndView mv = new ModelAndView();
		String page = request.getParameter("page");
		String boardNo = request.getParameter("boardNo");
		int no = Integer.parseInt(request.getParameter("no"));
		String page1=request.getParameter("pageNo");
		int recommend=this.boardService.recommendCount(no);
		int unrecommend=this.boardService.unrecommendCount(no);
		SubBoardListVO rlist=this.subBoardService.replyPaging(page1, no);
		if(page==null){
			page = "1";
		}
		mv.addObject("page", page);
		mv.addObject("boardNo", boardNo);
		mv.addObject("blist", boardService.getContentView(no));
		mv.addObject("rlist",rlist);
		mv.addObject("recommend",recommend);
		mv.addObject("unrecommend",unrecommend);
		mv.setViewName("viewinfo.board");
		
		return mv;
	}

	@RequestMapping("/getContentNo_board.do")
	public ModelAndView getContentViewNo(HttpServletRequest request, int no) throws Exception{
		ModelAndView mv = new ModelAndView();
		int boardNo = Integer.parseInt(request.getParameter("boardNo"));
		String page=request.getParameter("page");
		String page1=request.getParameter("pageNo");
		int recommend=this.boardService.recommendCount(no);
		int unrecommend=this.boardService.unrecommendCount(no);
		SubBoardListVO rlist=this.subBoardService.replyPaging(page1, no);
		if(page==null){
			page = "1";
		}
		mv.addObject("blist",boardService.getContentNo(no));
		mv.addObject("boardNo",boardNo);
		mv.addObject("page",page);
		mv.addObject("rlist",rlist);
		mv.addObject("recommend",recommend);
		mv.addObject("unrecommend",unrecommend);
		mv.setViewName("viewinfo.board");
		
		return mv;
	}
	@RequestMapping("/writeGo_board.do")
	public ModelAndView writeGo(HttpServletRequest request){
		String boardNo=request.getParameter("boardNo");
		String page=request.getParameter("page");
		ModelAndView mv=new ModelAndView();
		mv.addObject("boardNo",boardNo);
		mv.addObject("page",page);
		if(boardNo.equals("13")||boardNo.equals("20")){
			mv.setViewName("genrewrite.board");
		}else{
			mv.setViewName("write.board");
		}
		return mv;
	}
	@RequestMapping("/updateGo_board.do")
	public ModelAndView updateGo(HttpServletRequest request, int no) throws Exception{
		ModelAndView mv=new ModelAndView();
		String page = request.getParameter("page");
		String page1 = request.getParameter("pageNo");
		String boardNo = request.getParameter("boardNo");
		mv.addObject("page",page);
		mv.addObject("pageNo",page1);
		mv.addObject("boardNo",boardNo);
		mv.addObject("bvo",boardService.getContentNo(no));
		mv.setViewName("update.board");
		return mv;
	}
		@RequestMapping("/updateBoard_board.do")
	public ModelAndView updateBoard(HttpServletRequest request,BoardVO bvo,int no) throws Exception{
			ModelAndView mv=new ModelAndView();
			
			String page = request.getParameter("page");
			String boardNo = request.getParameter("boardNo");
			String page1=request.getParameter("pageNo");
			
			if(boardNo.equals("20")||boardNo.equals("13")){
				MultipartFile mfile=bvo.getUploadFile();
				if(mfile!=null&&mfile.isEmpty()==false){
					String fileName=mfile.getOriginalFilename();
					bvo.setOrgFileName(fileName);
					String newfilename=System.currentTimeMillis()+"_"+fileName;
					bvo.setNewFileName(newfilename);
					File copyFile=new File(path+newfilename);
					mfile.transferTo(copyFile);
				}
			}
			bvo.setBoardKind(boardNo);
			boardService.updateBoard(bvo);
			
			mv.addObject("page",page);
			mv.addObject("boardNo",boardNo);
			SubBoardListVO rlist=this.subBoardService.replyPaging(page1, no);
			
			mv.addObject("blist",boardService.getContentNo(bvo.getNo()));
			mv.addObject("rlist",rlist);
			mv.setViewName("viewinfo.board");
			return mv;
	}
		@RequestMapping("/deleteBoard_board.do")
		public ModelAndView deleteBoard(HttpServletRequest request, HttpServletResponse response, BoardVO bvo) throws Exception{
			boardService.deleteBoard(bvo.getNo());
			String page = request.getParameter("page");
			String boardNo = request.getParameter("boardNo");
			String newfilename=request.getParameter("newfilename");
			if(newfilename!=null&&newfilename.equals("")==false){
				deleteFile(newfilename);
			}
			return new ModelAndView("redirect:allView_board.do?boardNo="+boardNo+"&page="+page);
		}
		
		@RequestMapping("/searchBoard_board.do")
		public ModelAndView searchBoard(HttpServletRequest request, HttpServletResponse response) throws Exception{
			String select = request.getParameter("select");
			String content = request.getParameter("content");
			int boardNo = Integer.parseInt(request.getParameter("boardNo"));
			
			BoardListVO lvo = new BoardListVO();
			ModelAndView mv = new ModelAndView();
			
			String page = request.getParameter("page");
			boolean genreFlag = false;
			
			if(boardNo==13||boardNo==20){
				String genreName = request.getParameter("genreName");
				mv.setViewName("selectFindGenre.board");
				mv.addObject("boardName", boardService.getBoardNameByBoardNo(boardNo));
				mv.addObject("genreList", boardService.getGenreList());
				mv.addObject("genreName", genreName);
				genreFlag = true;
				lvo = boardService.getAllViewFindBySelect(page, boardNo, genreFlag, select, content, genreName);
			}else{
				mv.setViewName("selectFind.board");
				mv.addObject("boardName", boardService.getBoardNameByBoardNo(boardNo));
				lvo = boardService.getAllViewFindBySelect(page, boardNo, genreFlag, select, content, null);
			}
			mv.addObject("lvo", lvo);
			mv.addObject("boardNo", boardNo);
			return mv;
		}
	
	@RequestMapping("/readFail_board.do")
	public String readFail(){
		return "readFail.board";
	}
	
	@RequestMapping("/searchBoardGenre_board.do")
	public ModelAndView searchBoardGenre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int boardNo = 0;
		String genreName = request.getParameter("genreName");
		if(genreName!=null){
			boardNo = 20;
		}
		String page = request.getParameter("page");
		BoardListVO lvo = boardService.getAllViewFindByGenre(page, boardNo, genreName);
		ModelAndView mv = new ModelAndView();

		mv.addObject("boardName", boardService.getBoardNameByBoardNo(boardNo));
		mv.addObject("genreList", boardService.getGenreList());
		mv.addObject("genreName", genreName);
		mv.addObject("lvo", lvo);
		mv.addObject("boardNo", boardNo);
		mv.setViewName("selectFindGenre.board");
		return mv;
	}
	@RequestMapping("/fileDownload_board.do")
	public ModelAndView fileDownload(HttpServletRequest request,
			HttpServletResponse response,HttpSession session,String no) throws Exception{
		subBoardService.saveReport(no);
		MemberVO mvo=(MemberVO)session.getAttribute("vo");
		boardService.updatePointFileDown(mvo.getId());
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("path", path);
		map.put("orgfilename",request.getParameter("orgFileName"));
		map.put("newfilename",request.getParameter("newFileName"));
		map.put("no", no);
		session.setAttribute("vo", memberService.login(mvo));
		return new ModelAndView("downloadView",map);
	}
	
	@RequestMapping("/deleteUploadFile_board.do")
	@ResponseBody
	public boolean deleteUploadFile(HttpServletRequest request,
			HttpServletResponse response){
		boolean flag = false;
		String newfilename=request.getParameter("newFileName");
		if(newfilename!=null&&!newfilename.equals("")){
			this.deleteFile(newfilename);
			flag = true;
		}
		return flag;
	}
	@RequestMapping("/view_bigsize.do")
	public ModelAndView viewBigSize(HttpServletRequest request)
	{
		String newFileName = request.getParameter("newfilename");
		ModelAndView mv = new ModelAndView();
		mv.addObject("newFileName", newFileName);
		mv.setViewName("WEB-INF/view/board/bigsize");
		return mv;
	}
	@RequestMapping("/recommend_board.do")
	@ResponseBody
	public int recommend(HttpSession session, int no) throws Exception{
		MemberVO mvo=(MemberVO) session.getAttribute("vo");
		int result=this.boardService.recommend(mvo.getId(), no);	
		return result;
	}
	@RequestMapping("/unrecommend_board.do")
	@ResponseBody
	public int unrecommend(HttpSession session, int no) throws Exception{
		MemberVO mvo=(MemberVO) session.getAttribute("vo");
		int result=this.boardService.unrecommend(mvo.getId(), no);	
		return result;
	}
}
